import { createRouter, createWebHistory } from 'vue-router'
import amjRoutes from "@/router/amj/index"
import adminRoutes from "@/router/admin/index"
import { ElMessage } from 'element-plus';



const routes = [
  {
    path: '/',
    redirect: '/amjHome'
  },
  {
    path: '/amjHome',
    redirect: '/amjHome/frontHome',
    component: () => import("@/views/amjViews/amjHome.vue"),
    children: [
      ...amjRoutes,

    ]
  },
  {
    path: '/adminHome',
    redirect: '/adminHome/backHome',
    component: () => import("@/views/adminViews/adminHome.vue"),
    children: [
      ...adminRoutes
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import("@/views/login.vue"),
  },
  {
    path: '/rocket',
    name: 'rocket',
    component: () => import("@/components/rocket.vue"),
  },
  {
    path: '/personal',
    name: 'personal',
    component: () => import("@/views/personal.vue"),
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由登录守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("token")
  // 用户未登陆只允许访问登录页和注册页
  if (!token &&
    // ❗️ 避免无限重定向
    to.name !== 'login') {
    next('/login')
    ElMessage.warning('您还未登录！')
  } else {
    next()
  }
})

export default router
